/**
 ****************************************************************************************************
 * @file        freertos.c
 * @author      正点原子团队(ALIENTEK)
 * @version     V1.4
 * @date        2022-01-04
 * @brief       FreeRTOS 移植实验
 * @license     Copyright (c) 2020-2032, 广州市星翼电子科技有限公司
 */

#include "userConfig.h"
#include "usart.h"
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"

/* START_TASK 任务 配置
 * 包括: 任务句柄 任务优先级 堆栈大小 创建任务
 */
#define START_TASK_PRIO         1
#define START_TASK_STACK_SIZE   256
TaskHandle_t    start_task_handler;
TimerHandle_t oneShotTimerHandler = 0;    /* 单次定时器 */
TimerHandle_t periodTimerHandler = 0;    /* 周期定时器 */

/* timer1的超时回调函数 */
void timer1_callback(TimerHandle_t pxTimer)
{
    static uint32_t timer = 0;
    usart_transmit("OneShot Timer的运行次数: %d\r\n",++timer);
}

/* timer2的超时回调函数 */
void timer2_callback(TimerHandle_t pxTimer)
{
    static uint32_t timer = 0;
    usart_transmit("Period Timer的运行次数: %d\r\n",++timer);
}

static void start_task(void * pvParameters)
{
    taskENTER_CRITICAL();

    /* 单次定时器 */
    oneShotTimerHandler = xTimerCreate("timer1",
                                    500,
                                    pdFALSE,
                                    (void *)1,
                                    timer1_callback );

    /* 周期定时器 */
    periodTimerHandler = xTimerCreate("timer2",
                                    2000,
                                    pdTRUE,
                                    (void *)2,
                                    timer2_callback);

    xTimerStart(oneShotTimerHandler, portMAX_DELAY);
    xTimerStart(periodTimerHandler, portMAX_DELAY);

    vTaskDelete(NULL);
    taskEXIT_CRITICAL();
}

/**
 * @brief       FreeRTOS例程入口函数
 * @param       无
 * @retval      无
 */
void timer_demo(void)
{
    xTaskCreate((TaskFunction_t         )   start_task,
                (char *                 )   "start_task",
                (configSTACK_DEPTH_TYPE )   START_TASK_STACK_SIZE,
                (void *                 )   NULL,
                (UBaseType_t            )   START_TASK_PRIO,
                (TaskHandle_t *         )   &start_task_handler);
}
